$(function(){
	
	$("#wrapper").css({
		'height' : $(document).height()
	});

	$("#menu .category").click(function(){
		$("#menu .category .options").each(function(){
			$(this).slideUp(500);
		});

		if($(this).find(".options").css("display") == 'none')
		{
			$(this).find(".options").slideDown(500);
		}
		else
		{
			$(this).find(".options").slideUp(500);
		}
	});

	$("#sidemenu .category").click(function(){
		$("#sidemenu .category .options").each(function(){
			$(this).slideUp(500);
		});

		if($(this).find(".options").css("display") == 'none')
		{
			$(this).find(".options").slideDown(500);
		}
		else
		{
			$(this).find(".options").slideUp(500);
		}
	});

	$("form[name='mlglogin'] .btnregister").click(function(){
		window.location = '?page=register';
		return false;
	});

	$("form[name='show'] .btnreact").click(function(){
		window.location = '?page=messages&id=';
		return false;
	});

	$("select[name='destination']").change(function(){
		if($(this).val() == '3')
		{
			$("td.hidden").show();
		}
		else
		{
			$("td.hidden").hide();	
		}
	});

	if($("select[name='destination']").val() == '3')
	{
		$("td.hidden").show();
	}

	$("td.message img").removeAttr('style');
	$("td.bio img").removeAttr('style');

	//register
	$(".register .registerbtn").click(function(){


		error = 0;

		uname = $(".register input[name='username']").val();
		email = $(".register input[name='email']").val();

		password = $(".register input[name='password1']").val();
		rpassword = $(".register input[name='password2']").val();

		fname = $(".register input[name='fname']").val();
		lname = $(".register input[name='lname']").val();

		cname = $(".register input[name='cname']").val();

		cpic = $(".register input[name='character']:radio:checked").val();

		if(uname == '' || uname == null || 
			fname == '' || fname == null || 
			lname == '' || lname == null || 
			cname == '' || cname == null || 
			password == '' || password == null || 
			rpassword == '' || rpassword == null || 
			email == '' || email == null )
		{
			error = 1;
		}

		if(error == 1)
		{
			$(".registeroutput").slideUp(300, function(){
				$(".registeroutput").html("Please fill in all of the fields.");
				$(".registeroutput").slideDown(300);
			});
			return false;
		}
		else
		{
			errorstring = 'Some fields are invalid:<br>';

			emailpreg = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
				
			namepreg = /^([a-zA-Z ])+$/;
			unamepreg = /^([a-zA-Z0-9_-])+$/;

			if(!emailpreg.test(email))
			{
				error = 2;
				errorstring += '- Invalid email.<br>';
			}

			if(!unamepreg.test(uname))
			{
				error = 2;
				errorstring += '- Username may only contain letters and numbers.<br>';
			}

			if(!unamepreg.test(cname))
			{
				error = 2;
				errorstring += '- Charactername may only contain letters and numbers.<br>';
			}

			if(!namepreg.test(fname))
			{
				error = 2;
				errorstring += '- First name may only contain letters and spaces.<br>';
			}

			if(!namepreg.test(lname))
			{
				error = 2;
				errorstring += '- Last name may only contain letters and spaces.<br>';
			}
			
			if(password != rpassword)
			{
				error = 2;
				errorstring += '- Password don\'t match.<br>';
			}

			if(uname.length < 4)
			{
				error = 2;
				errorstring += '- Username must be at least 4 characters.<br>';
			}

			if(cname.length < 4)
			{
				error = 2;
				errorstring += '- Charactername must be at least 4 characters.<br>';
			}

			if(password.length < 6)
			{
				error = 2;
				errorstring += '-Password must be at least 6 characters.<br>';
			}

			if(cname.length > 12)
			{
				error = 2;
				errorstring += '- Charactername can\'t be longer than 12 characters.<br>';
			}

			if(error == 2)
			{
				$(".registeroutput").slideUp(300, function(){
					$(".registeroutput").html(errorstring);
					$(".registeroutput").slideDown(300);
				});
				return false;
			}
			else
			{
				
				errorstring2 = 'Some fields are invalid:<br>';

				udata = "udata=" + uname;
				cdata = "cdata=" + cname;
				edata = "edata=" + email;


	          	$.ajax({  
	            	async: false, cache: false, type: "POST", url: "functions/func_register.php?checkuser", datatype: "html", data: udata,  
	            }).done(function(data){
	              	if(data != 0)
	              	{
						errorstring2 += "- Username already exists.<br>";
						error = 1;
	              	}
	          	});

	          	$.ajax({  
	            	async: false, cache: false, type: "POST", url: "functions/func_register.php?checkemail", datatype: "html", data: edata,  
	          	}).done(function(data){
	          		if(data != 0)
	              	{
						errorstring2 += "- An account with this email already exists.<br>";
						error = 1;
	              	}
	          	});

	          $.ajax({  
	            	async: false, cache: false, type: "POST", url: "functions/func_register.php?checkchar", datatype: "html", data: cdata,  
	            }).done(function(data){
	              	if(data != 0)
	              	{
						errorstring2+= "- Charactername already exists.<br>";
						error = 1;
	              	}
	          	});

	            if(error == 1)
	          	{
	          		$(".registeroutput").slideUp(300, function(){
						$(".registeroutput").html(errorstring2);
						$(".registeroutput").slideDown(300);
					});
					return false;
	          	}
	          	else
	          	{
	          		data = "username=" + uname + "&password=" + password + "&email=" + email + "&fname=" + fname + "&lname=" + lname + "&cname=" + cname + "&cpic=" + cpic;

	          		$.ajax({  
		            	type: "POST", 
		            	url: "functions/func_register.php?register", 
		            	datatype: "html", 
		            	data: data,  
		            	success: function(data){
		            		$(".registeroutput").slideUp(300, function(){
		            			$(".registerform").slideUp(400, function(){
									$(".registeroutput").html(data);
									$(".registeroutput").slideDown(300);
								});

		            		});

		            		
		            	}
		            });
	          		return false;
	          	}

			}
		}



	});
});